package com.common.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import org.apache.commons.net.telnet.TelnetClient;

import com.common.util.BrowserSetupConfig;
import com.common.util.ProcessUtil;

	/**
	@author 王嘉犇
	@name 杀死相关浏览器进程
	 */

public class KillProcess {
	
    static InputStream in;
    
    static PrintStream out;
    
    static String prompt;
    
    static TelnetClient telnet;
	
    /**
     * 杀进程操作
     */
    public static void killprocess() throws Exception{
        Runtime rn = Runtime.getRuntime();
        @SuppressWarnings("unused")
		Process p;
        if(BrowserSetupConfig.REMOTEORNOT==0){
            switch (BrowserSetupConfig.BROWSER.toUpperCase()){
                case "IE" :
                    try {
                        p = rn.exec("cmd /c taskkill /f /im " + ProcessUtil.IEPROCESS);
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                    break;
                case "FIREFOX" :
                    try {
                        p = rn.exec("cmd /c taskkill /f /im " + ProcessUtil.FIREFOXPROCESS);
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                    break;
                case "CHROME" :
                    try {
                        p = rn.exec("cmd /c taskkill /f /im " + ProcessUtil.CHROMEPROCESS);
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                    break;
                default:
                	handlefailure("没有"+ BrowserSetupConfig.BROWSER.toUpperCase() + "这种浏览器类型");
                    break;
            }
        }else if(BrowserSetupConfig.REMOTEORNOT==1){
        	telnet = new TelnetClient("VT220");
        	telnet.connect(BrowserSetupConfig.REMOTEIP, BrowserSetupConfig.REMOTEPORT);
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream());
            prompt = ">";
        	login(BrowserSetupConfig.REMOTEUSERNAME, BrowserSetupConfig.REMOTEPASSWORD);
        	switch (BrowserSetupConfig.BROWSER.toUpperCase()){
        		case "IE" :
        			sendCommand("cmd /c taskkill /f /im " + ProcessUtil.IEPROCESS);
        			break;
        		case "FIREFOX" :
        			sendCommand("cmd /c taskkill /f /im " + ProcessUtil.FIREFOXPROCESS);
        			break;
        		case "CHROME" :
        			sendCommand("cmd /c taskkill /f /im " + ProcessUtil.CHROMEPROCESS);
        			break;
        		default :
        			handlefailure("没有"+ BrowserSetupConfig.BROWSER.toUpperCase() + "这种浏览器类型");
        			break;
        	}
        	disconnect();
        }else{
        	handlefailure("模式错误");
        }
    }
    
    private static void login(String user, String password) {
        readUntil("\rlogin: ");
        write(user);
        readUntil("\rpassword: ");
        write(password);
        readUntil(prompt + "");
    }
    
    private static String readUntil(String pattern) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            int bufferSize = 32;
            final byte[] buf = new byte[bufferSize];
            int read = 0;
            while(read != -1){
                read = in.read(buf);
                try {
					outputStream.write(buf, 0, read);
				} catch (IndexOutOfBoundsException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                String str = outputStream.toString("gb2312");
                if(str.endsWith(pattern)){
                    return str;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    private static void write(String value) {
        try {
            out.println(value);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static String sendCommand(String command) {
        try {
            write(command);
            return readUntil(prompt + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    private static void disconnect() {
        try {
            telnet.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void handlefailure(String msg) throws Exception{
    	throw new Exception(msg);
    }
}
